﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace zmMoniter
{
    public partial class frmAddUser : DevExpress.XtraEditors.XtraForm
    {
        public mainForm main = null;
        public frmAddUser(mainForm frm)
        {
            InitializeComponent();
            this.main = frm;
        }

        private void sBtn_Add_Click(object sender, EventArgs e)
        {
            if (tEdt_UserName.Text == "")
            {
                lab_Msg.Text = "消息：用户名不能为空!";
                return;
            }
            if (tEdt_LoginName.Text == "")
            {
                lab_Msg.Text = "消息：登录名不能为空!";
                return;
            }
            if (tEdt_Psw1.Text == "")
            {
                lab_Msg.Text = "消息：密码不能为空!";
                return;
            }
            if (tEdt_Psw2.Text == "")
            {
                lab_Msg.Text = "消息：密码验证不能为空!";
                return;
            }
            if (tEdt_Psw1.Text.Length < 6)
            {
                lab_Msg.Text = "消息：密码长度不能少于6位!";
                return;
            }
            if (tEdt_Psw1.Text.Length > 8)
            {
                lab_Msg.Text = "消息：密码长度不能超过8位!";
                return;
            }
            if (tEdt_Psw2.Text != tEdt_Psw1.Text)
            {
                lab_Msg.Text = "消息：两次输入密码不一致!";
                tEdt_Psw1.Text = "";
                tEdt_Psw2.Text = "";
                return;
            }
            if (cBox_Purview.Text != "管理员" && cBox_Purview.Text != "操作员")
            {
                lab_Msg.Text = "消息：请选择用户角色权限!";
                return;
            }
            if (!addUser())
            {
                lab_Msg.Text = "消息：添加用户失败!";
                return;
            }
        }
        private bool addUser()
        {
            bool bRst = false;


         
            string purview = cBox_Purview.Text == "管理员" ? "1" : "2";
            string sMD5Psw = MD5Hashing.HashString(tEdt_Psw1.Text);

            string sSQL = "INSERT INTO 用户 (ID,用户名,密码,权限,移动电话," +
             "EMAIL,登陆名,是否在线)" +
             "VALUES (用户自增长.nextval"+",'" + tEdt_UserName.Text + "','" + sMD5Psw + "'," + purview +
             ",'" + tEdt_Tel.Text +
             "','" + tEdt_EMail.Text + "','" + tEdt_LoginName.Text + "',0)";
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            int iResult = db.ExecuteSQL(sSQL);
            if (iResult > 0)
            {
                MessageBox.Show("添加用户成功！", "添加用户提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reSetEdit();
                bRst = true;
                this.Hide();
            }
            else
                MessageBox.Show("添加用户失败！", "添加用户提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            db.CloseConn();

            return bRst;
        }
        private void reSetEdit()
        {
            tEdt_UserName.Text = "";
            tEdt_LoginName.Text = "";
            tEdt_Psw1.Text = "";
            tEdt_Psw2.Text = "";
            cBox_Purview.Text = "选择角色权限";
            tEdt_Tel.Text = "";
            tEdt_EMail.Text = "";
            lab_Msg.Text = "";
        }
    }
}